#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        printf("usage: fastChangeCore pid startCoreId endCoreId\n");
        return 1;
    }
    cpu_set_t mask;

    int pid = atoi(argv[1]);
    int startCoreId = atoi(argv[2]);
    int endCoreId = atoi(argv[3]);
    int coreId = startCoreId;
    while (1)
    {
        CPU_ZERO(&mask);
        CPU_SET(coreId, &mask);
        // 进程迁移到CPU i
        if (sched_setaffinity(pid, sizeof(mask), &mask) == -1)
        {
            printf("绑定CPU出错, ERROR:%s\n", strerror(errno));
            return -1;
        }
        coreId++;
        if (coreId > endCoreId)
        {
            coreId = startCoreId;
        }
    }
    return 0;
}